package io.gitee.zhangbinhub.admin.component

import cn.dev33.satoken.stp.StpInterface
import io.gitee.zhangbinhub.admin.constant.AcpConstant
import io.gitee.zhangbinhub.admin.tools.TokenTools
import org.springframework.stereotype.Component

@Component
class AuthPermission(private val tokenTools: TokenTools) : StpInterface {
    override fun getPermissionList(loginId: Any, loginType: String?): MutableList<String> =
        tokenTools.getAccessTokenModel()?.let { atm ->
            if (atm.loginId == loginId) {
                atm.extraData[AcpConstant.tokenPermissions].let { permissionList ->
                    if (permissionList != null && permissionList is MutableList<*>) {
                        permissionList.map { it.toString() }.toMutableList()
                    } else {
                        mutableListOf()
                    }
                }
            } else {
                mutableListOf()
            }
        } ?: mutableListOf()

    override fun getRoleList(loginId: Any, loginType: String?): MutableList<String> =
        tokenTools.getAccessTokenModel()?.let { atm ->
            if (atm.loginId == loginId) {
                atm.extraData[AcpConstant.tokenRoles].let { permissionList ->
                    if (permissionList != null && permissionList is MutableList<*>) {
                        permissionList.map { it.toString() }.toMutableList()
                    } else {
                        mutableListOf()
                    }
                }
            } else {
                mutableListOf()
            }
        } ?: mutableListOf()
}